<?php
    class GP_backup
    {
        function backup()
        {
            $sql = "";
            $db = db();
            $q = 'SHOW TABLES FROM '. config('db.name');
            $tables = $db->fetchArray($q);
            foreach ($tables as $table) {
                $tableName = $table['Tables_in_' . config('db.name')];
                $q = 'SHOW CREATE TABLE '. $tableName;
                $res = $db->fetchArray($q);
                if (count($res)) {
                    foreach ($res as $createTable) {
                        $dumpCreate = $createTable['Create Table'];
                        $sql .= $dumpCreate . ";\n\n";
                    }
                    $q = "SELECT * FROM $tableName";
                    $res = $db->fetchArray($q);
                    if (count($res)) {
                        foreach ($res as $row) {
                            $sql .= "INSERT INTO $tableName SET ";
                            foreach ($row as $key => $value) {
                                $sql .= "$key = '" . sanitsql($value) . "', ";
                            }
                            $sql = substr($sql, 0, -2);
                            $sql .= ";\n\n";
                        }
                    }
                }
            }
            $file = date('d_m_Y_H_i_s') . '_' . getvar('**module**') . '.sql.gz';
            $fileSql = DIR_BACKUPS . $file;
            
            $urlSql = getvar('**urlSite**') . '_backups_/' . $file;
            
            compressFile($fileSql, $sql);
            
            $bigFile = new GP_bigfile($urlSql, 'Backup SQL');
            $urlFile = mediaLink($bigFile->_id);
            mail(config('mailToBackup'), config('mailSubjectBackup'), $urlFile, "From: backup<" . config('mailFromBackup') . ">");
            // @unlink($fileSql);
        }
    }
?>